//
//  BJArticleViewController.m
//  BowJoy
//
//  Created by  on 2024/5/13.
//

#import "BJArticleViewController.h"
#import "BJHomeListTableViewCell.h"
#import "BJAnchorManager.h"
#import "BJFMDBManager.h"
#import "BJArticleInfoViewController.h"

@interface BJArticleViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong,nonatomic) NSMutableArray<BJAnchorWallModelData *> *records;

@property (weak, nonatomic) IBOutlet UIView *bgGoundView;


@end

@implementation BJArticleViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.naviTitle = @"Explore";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.tableView registerNib:[UINib nibWithNibName:@"BJHomeListTableViewCell" bundle:nil] forCellReuseIdentifier:@"BJHomeListTableViewCell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;

}
- (void)viewWillAppear:(BOOL)animated{
    NSArray *dataQueryList = [[BJFMDBManager sharedManager] queryAllData];
    if (dataQueryList.count>0) {
        self.records = [[NSMutableArray alloc]initWithArray:dataQueryList];
        [self.tableView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.records.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BJHomeListTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"BJHomeListTableViewCell"];
    if (cell == nil){
        cell = [[BJHomeListTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BJHomeListTableViewCell"];
    }
    
    BJAnchorWallModelData *model = self.records[indexPath.row];
    cell.model = model;
    kWeakSelf(weakSelf)

    cell.moreBtn.onClick(^{
        ReportBlock *reportBlock =[[ReportBlock alloc]initWithIsBlock:model.isFriend isFriend:model.isFriend userId:[model.userId longLongValue]];
        [reportBlock setFollowClickBlock:^(BOOL follow) {
            model.isFriend = follow;
            if (follow) {
                [BJUserManager sharedManager].followingNum ++;
            }else{
                [BJUserManager sharedManager].followingNum --;
            }
            [[BJFMDBManager sharedManager] uptadeDataWithFrend:follow andUserId:model.userId];
            [weakSelf.tableView reloadData];
        }];//关注
        [reportBlock setBlockClickBlock:^(BOOL block) {
            [weakSelf.records removeObject:model];
            [weakSelf.tableView reloadData];
            [BJUserManager sharedManager].blockNum ++;

            [[BJFMDBManager sharedManager] deleteDataWithUserId:model.userId];
            [[BJFMDBBlockManager sharedManager] insertWitModel:model];
                    
        }];//拉黑
        
        [reportBlock show];

    });
    cell.followBtn.onClick(^{
        if (!model.isLike) {
            model.isLike = YES;
            model.followNum ++;
            [weakSelf.tableView reloadData];
        }else{
            model.isLike = NO;
            model.followNum --;
            [weakSelf.tableView reloadData];
        }
        [[BJFMDBManager sharedManager] uptadeDataWithLike:model.isLike andUserId:model.userId];

    });
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return  cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    BJArticleInfoViewController *vc = [[BJArticleInfoViewController alloc] init];
    vc.model = self.records[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];

}


@end
